package org.laizili.misc;


import java.util.Arrays;

public class Rearrange {
    // 把负数排在数组左边，整数排在数组右边，不改变正数（负数）间的相对位置
    public static void rearrange(int[] arr) {
        int lastNegativeIndex = -1;
        for (int i = 0; i < arr.length - 1; i++) {
            if (arr[i] < 0) {
                int j = i - 1;
                int tmp = arr[i];
                while (j > lastNegativeIndex) {
                    arr[j + 1] = arr[j];
                    j--;
                }
                arr[++lastNegativeIndex] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        final int[] ints = {-2, 3, 11, 7, -4, -22, 5};
        rearrange(ints);
        System.out.println(Arrays.toString(ints));
    }
}
